package com.eshop.tools.express;

import org.springframework.util.Base64Utils;
import java.security.MessageDigest;
import cn.hutool.http.HttpUtil;
import java.net.URLEncoder;
import java.util.HashMap;
import com.eshop.enums.ShipperCodeEnum;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.eshop.tools.express.dao.ExpressInfo;
import java.util.Iterator;
import java.util.Map;
import org.apache.commons.logging.LogFactory;
import com.eshop.tools.express.config.ExpressProperties;
import org.apache.commons.logging.Log;
import java.io.Serializable;

public class ExpressService implements Serializable
{
    private final Log logger;
    private String ReqURL;
    private ExpressProperties properties;
    
    public ExpressService() {
        this.logger = LogFactory.getLog((Class)ExpressService.class);
        this.ReqURL = "http://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx";
    }
    
    public ExpressProperties getProperties() {
        return this.properties;
    }
    
    public void setProperties(final ExpressProperties properties) {
        this.properties = properties;
    }
    
    public String getVendorName(final String vendorCode) {
        for (final Map<String, String> item : this.properties.getVendors()) {
            if (item.get("code").equals(vendorCode)) {
                return item.get("name");
            }
        }
        return null;
    }
    
    public ExpressInfo getExpressInfo(final String OrderCode, final String ShipperCode, final String LogisticCode, final String lastFourNumber) {
        try {
            final String result = this.getOrderTracesByJson(OrderCode, ShipperCode, LogisticCode, lastFourNumber);
            final ObjectMapper objMap = new ObjectMapper();
            final ExpressInfo ei = (ExpressInfo)objMap.readValue(result, (Class)ExpressInfo.class);
            ei.setShipperName(this.getVendorName(ShipperCode));
            return ei;
        }
        catch (Exception e) {
            this.logger.error((Object)e.getMessage(), (Throwable)e);
            return null;
        }
    }
    
    private String getOrderTracesByJson(final String OrderCode, final String ShipperCode, final String LogisticCode, final String lastFourNumber) throws Exception {
        if (!this.properties.isEnable()) {
            return null;
        }
        String requestData;
        if (ShipperCode.equals(ShipperCodeEnum.SF.getValue())) {
            requestData = "{'OrderCode':'" + OrderCode + "','ShipperCode':'" + ShipperCode + "','LogisticCode':'" + LogisticCode + "','CustomerName':'" + lastFourNumber + "'}";
        }
        else {
            requestData = "{'OrderCode':'" + OrderCode + "','ShipperCode':'" + ShipperCode + "','LogisticCode':'" + LogisticCode + "'}";
        }
        final Map<String, Object> params = new HashMap<String, Object>();
        params.put("RequestData", URLEncoder.encode(requestData, "UTF-8"));
        params.put("EBusinessID", this.properties.getAppId());
        params.put("RequestType", "1002");
        final String dataSign = this.encrypt(requestData, this.properties.getAppKey(), "UTF-8");
        params.put("DataSign", URLEncoder.encode(dataSign, "UTF-8"));
        params.put("DataType", "2");
        final String result = HttpUtil.post(this.ReqURL, (Map)params);
        return result;
    }
    
    private String MD5(final String str, final String charset) throws Exception {
        final MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(str.getBytes(charset));
        final byte[] result = md.digest();
        final StringBuilder sb = new StringBuilder(32);
        for (int i = 0; i < result.length; ++i) {
            final int val = result[i] & 0xFF;
            if (val <= 15) {
                sb.append("0");
            }
            sb.append(Integer.toHexString(val));
        }
        return sb.toString().toLowerCase();
    }
    
    private String encrypt(String content, final String keyValue, final String charset) {
        if (keyValue != null) {
            content += keyValue;
        }
        try {
            final byte[] src = this.MD5(content, charset).getBytes(charset);
            return Base64Utils.encodeToString(src);
        }
        catch (Exception e) {
            this.logger.error((Object)e.getMessage(), (Throwable)e);
            return null;
        }
    }
}
